import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleInfoDTO, ServiceProxy } from '@shared/service-proxy/service-proxies';
import { Component, OnInit, Output, EventEmitter, Injector } from '@angular/core';
import { FormBuilder } from '@angular/forms';

@Component({
  selector: 'app-set-role-remark-modal',
  templateUrl: './set-role-remark-modal.component.html',
  styleUrls: ['./set-role-remark-modal.component.less']
})
export class SetRoleRemarkModalComponent extends AppComponentBase implements OnInit {
  @Output() modalSave = new EventEmitter<any>();
  isVisible = false;
  isOkLoading = false;
  role: RoleInfoDTO;
  title: string;
  constructor(injector: Injector, private _service: ServiceProxy, private fb: FormBuilder) {
    super(injector)
  }

  ngOnInit() {
    this.role = new RoleInfoDTO();
  }

  handleCancel() {
    this.isVisible = false;
  }

  handleOk() {
    this.isOkLoading = true;
    this._service.updateRoleInfoStatusUsingPUT(this.role.id, this.role).subscribe(result => {
      if (result.success) {
        this.modalSave.emit();
        this.isVisible = false;
        this.isOkLoading = false;
        this.message.success(this.l("save_success"));
      }
      else
        this.message.info(this.l(result.error.message));
    }, error => {
      this.isOkLoading = false;
      this.message.error(this.l(error.message));
    });
  }
  show(id: string, usedFlag: boolean) {
    this.isVisible = true;
    this.role.id = id;
    this.role.usedFlag = !usedFlag;
    if (this.role.usedFlag)
      this.title = this.l("enable");
    else
      this.title = this.l("disenable");
    this.role.remark = "";
  }

}
